/**   
* @Title: DaiHouYwServiceImpl.java 
* @Package com.zjdiepu.www.biz.zpp 
* 
* @author zhangpp
* @date 2017年5月26日 下午8:44:48 
* @version V1.0   
*/
package com.zjdiepu.www.biz.zpp;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.ZhppDateUtil;
import com.zjdiepu.www.dao.dh.DhDepositMoneyMapper;
import com.zjdiepu.www.dao.dh.DhOverdueListMapper;
import com.zjdiepu.www.dao.dh.DhRemindLoanMapper;
import com.zjdiepu.www.dao.dh.DhRemindLoanSubMapper;
import com.zjdiepu.www.dao.fk.FkAuditListMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.dao.zpp.DaiHouYwDao;
import com.zjdiepu.www.dao.zpp.PublicDao;
import com.zjdiepu.www.ibiz.zpp.DaiHouYwService;
import com.zjdiepu.www.ibiz.zpp.DaiZhongYwService;
import com.zjdiepu.www.model.dh.dto.DhDepositMoney;
import com.zjdiepu.www.model.dh.dto.DhOverdueList;
import com.zjdiepu.www.model.dh.dto.DhRemindLoan;
import com.zjdiepu.www.model.dh.dto.DhRemindLoanSub;
import com.zjdiepu.www.model.fk.dto.FkAuditList;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.zpp.dhyw.ext.DepositMoneyExt;
import com.zjdiepu.www.model.zpp.dhyw.vo.KeHuTuiYaJinListVo;
import com.zjdiepu.www.model.zpp.dzyw.vo.FkAuditListVo;
import com.zjdiepu.www.model.zpp.pub.ext.ParamsExt;

/**      
 * 项目名称：diepu-parent-biz   
 * 类名称：DaiHouYwServiceImpl   
 * 类描述：   
 * 创建人：zhangpp
 * 创建时间：2017年5月26日 下午8:44:48   
 * 修改人：zhangpp
 * 修改时间：2017年5月26日 下午8:44:48   
 * 修改备注：   
 * @version V1.0     
 */
@Service
public class DaiHouYwServiceImpl implements DaiHouYwService
{
	@Autowired
	private DaiHouYwDao daiHouYwDao;
	
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	
	@Autowired
	private DhDepositMoneyMapper dhDepositMoneyMapper;
	
	@Autowired
	private FkAuditListMapper fkAuditListMapper;
	
	@Autowired
	private DaiZhongYwService daiHouYwService;
	
	@Autowired
	private DhOverdueListMapper dhOverdueListMapper;
	
	@Autowired
	private DhRemindLoanMapper dhRemindLoanMapper;
	
	@Autowired
	private DhRemindLoanSubMapper dhRemindLoanSubMapper;
	
	@Autowired
	private PublicDao publicDao;

	/* 
	* @author zhangpp
	* <p>Title: daiHouYwKeHuTuiKuan</p> 
	* <p>Description: </p> 
	* @param depositMoneyExt
	* @param currentUser 
	* @see com.zjdiepu.www.ibiz.zpp.DaiHouYwService#daiHouYwKeHuTuiKuan(com.zjdiepu.www.model.examine.ext.DepositMoneyExt, com.zjdiepu.www.base.core.AbstractSysUser) 
	* @version V1.0
	*/
	@Override
	public void diepuDaiHouYwKeHuTuiKuan(DepositMoneyExt depositMoneyExt,AbstractSysUser currentUser) {
		
		
		String id = currentUser.getId();
		String name = currentUser.getName();
		
		Date date = ZhppDateUtil.strToTimestamp(ZhppDateUtil.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
		
		//业务主键
		String busiId = depositMoneyExt.getYwid();

		YwBusiness ywBusiness = ywBusinessMapper.selectByPrimaryKey(busiId);
		
		if(null == ywBusiness)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"ywBusinessMapper selectByPrimaryKey is null");
		}
		
		//修改  信息表的状态  350
//		ywBusiness.setBusiStatus(350);//去掉修改状态 modified by zzj
		ywBusiness.initUpdateData(id,name);
		int count = ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		if(count == 0)
		{
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),"ywBusinessMapper updateByPrimaryKeySelective error");
		} 
		
		//插入  dh_deposit_money  表数据
		DhDepositMoney dhDepositMoney = new DhDepositMoney();
		dhDepositMoney.initInsertData(id,name);
		dhDepositMoney.setBusiId(ywBusiness.getId());
		dhDepositMoney.setCusId(ywBusiness.getCustomerId());
		dhDepositMoney.setTypeRefund(depositMoneyExt.getTypeRefund());
		dhDepositMoney.setAmountApply(depositMoneyExt.getAmountApply());
		dhDepositMoney.setBank(depositMoneyExt.getBank());
		dhDepositMoney.setDepartmentRefund(depositMoneyExt.getDepartmentRefund());
		dhDepositMoney.setCardnoRefund(depositMoneyExt.getCardnoRefund());
		dhDepositMoney.setTimeRemit(date);
		dhDepositMoney.setAmountRefund(depositMoneyExt.getAmountRefund());
		dhDepositMoney.setTypeRemit(depositMoneyExt.getTypeRemit());
		dhDepositMoney.setOperatorRemit(id);
		dhDepositMoney.setTimeUpdateRemit(date);
		dhDepositMoney.setOperatorNameRemit(name);
		dhDepositMoney.setStatusRefundDeposit(10);
    	dhDepositMoney.setModifier(id); //统一使用修改人和修改人名称来初始化
    	dhDepositMoney.setModifierName(name);
    	dhDepositMoney.setRemarks(depositMoneyExt.getRemarks());
		count = dhDepositMoneyMapper.insert(dhDepositMoney);
		if(count < 1)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"dhDepositMoneyMapper insert error");
		} 

		//查询是否有逾期的数据
		DhOverdueList dhOverdueList = new DhOverdueList();
		dhOverdueList.setBusiId(busiId);
		dhOverdueList.setDpStatus(1);
		dhOverdueList = dhOverdueListMapper.selectRefined(dhOverdueList);
		
		//插入退押金催收审核
		if(null == dhOverdueList || ("").equals(dhOverdueList))
		{
			//查询审核记录
			FkAuditList auditList = new FkAuditList();
			auditList.setBusiId(busiId);
			auditList.setDpStatus(1);
			auditList = fkAuditListMapper.selectRefined(auditList);
			if(null != auditList && !("").equals(auditList))
			{
				//插入审核记录
				auditList.initInsertData(id, name);
				auditList.setModeId(ModeIDType.TYJXBSHH.getCode());
				auditList.setSource("客户申请退押金");
				auditList.setStatusList(1);
				auditList.setOperatorId("");
				auditList.setOperatorName("");
				count = fkAuditListMapper.insert(auditList);
				if(count < 1)
				{
					throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"fkAuditListMapper insert fail");
				} 
				
				return;
			}
			
			ParamsExt paramsExt = new ParamsExt();
			paramsExt.setBusiId(busiId);
			FkAuditListVo auditListVo  = daiHouYwService.getYwBusinessSaveFkAuditList(paramsExt);
			if(null == auditListVo || ("").equals(auditListVo))
			{
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),"auditListVo is null");
			}
			auditList = new FkAuditList();
			auditList.initInsertData(id, name);
			auditList.setModeId(ModeIDType.TYJXBSHH.getCode());
			auditList.setSource("客户申请退押金");
			auditList.setBusiId(busiId);
			auditList.setBusiConfirmTime(auditListVo.getBusi_confirm_time());
			auditList.setLoanAmount(auditListVo.getLoan_amount());
			auditList.setChannelStatus(Integer.valueOf(auditListVo.getChannel_status()));
			auditList.setChannelStatusName(auditListVo.getChannel_status_name());
			auditList.setChannelType(auditListVo.getChannel_type());
			auditList.setChannelTypeName(auditListVo.getChannel_type_name());
			auditList.setDealer(auditListVo.getDealer());
			auditList.setDepartmentName(auditListVo.getDepartment_name());
			auditList.setSalersmanName(auditListVo.getSalersman_name());
			auditList.setCusName(auditListVo.getCus_name());
			auditList.setCusIdno(auditListVo.getCus_idno());
			auditList.setBankname(auditListVo.getBankname());
			auditList.setOperateStatus(auditListVo.getOperate_status());
			auditList.setAssigningTaskType(auditListVo.getAssigning_task_type());
			auditList.setStatusList(1);
			auditList.setTimeAuditFinish(date);
			count = fkAuditListMapper.insert(auditList);
			if(count < 1)
			{
				throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"fkAuditListMapper insert is fail");
			} 
			return;
		}
		
		
		//查询审核记录
		FkAuditList auditList = new FkAuditList();
		auditList.setBusiId(busiId);
		auditList.setDpStatus(1);
		auditList = fkAuditListMapper.selectRefined(auditList);
		if(null != auditList && !("").equals(auditList))
		{
			//退押金续保审核
			auditList.initInsertData(id, name);
			auditList.setModeId(ModeIDType.TYJXBSHH.getCode());
			auditList.setSource("客户申请退押金");
			auditList.setStatusList(1);
			auditList.setOperatorId("");
			auditList.setOperatorName("");
			count = fkAuditListMapper.insert(auditList);
			if(count < 1)
			{
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),"fkAuditListMapper is fail");
			} 
			
			return;
		}
		
		ParamsExt paramsExt = new ParamsExt();
		paramsExt.setBusiId(busiId);
		FkAuditListVo auditListVo  = daiHouYwService.getYwBusinessSaveFkAuditList(paramsExt);
		if(null == auditListVo || ("").equals(auditListVo))
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"auditListVo is null");
		}
		auditList = new FkAuditList();
		auditList.initInsertData(id, name);
		auditList.setModeId(ModeIDType.TYJXBSHH.getCode());
		auditList.setSource("客户申请退押金");
		auditList.setBusiId(busiId);
		auditList.setBusiConfirmTime(auditListVo.getBusi_confirm_time());
		auditList.setLoanAmount(auditListVo.getLoan_amount());
		auditList.setChannelStatus(Integer.valueOf(auditListVo.getChannel_status()));
		auditList.setChannelStatusName(auditListVo.getChannel_status_name());
		auditList.setChannelType(auditListVo.getChannel_type());
		auditList.setChannelTypeName(auditListVo.getChannel_type_name());
		auditList.setDealer(auditListVo.getDealer());
		auditList.setDepartmentName(auditListVo.getDepartment_name());
		auditList.setSalersmanName(auditListVo.getSalersman_name());
		auditList.setCusName(auditListVo.getCus_name());
		auditList.setCusIdno(auditListVo.getCus_idno());
		auditList.setBankname(auditListVo.getBankname());
		auditList.setOperateStatus(auditListVo.getOperate_status());
		auditList.setAssigningTaskType(auditListVo.getAssigning_task_type());
		auditList.setStatusList(1);
		auditList.setTimeAuditFinish(date);
		count = fkAuditListMapper.insert(auditList);
		if(count < 1)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"fkAuditListMapper insert is fail");
		} 
	}

	/* 
	* @author zhangpp
	* <p>Title: getKeHuTuiYaJinList</p> 
	* <p>Description: </p> 
	* @param paramsExt
	* @return 
	* @see com.zjdiepu.www.ibiz.zpp.DaiHouYwService#getKeHuTuiYaJinList(com.zjdiepu.www.model.zpp.pub.ext.ParamsExt) 
	* @version V1.0
	*/
	@Override
	public KeHuTuiYaJinListVo getKeHuTuiYaJinList(ParamsExt paramsExt) {
		
		return daiHouYwDao.getKeHuTuiYaJinList(paramsExt);
	}

	/*  
	*  @author  zhangpp
	*  <p>Title:  diepuFangKuanHuanKuan</p>  
	*  <p>Description:  </p>  
	*  @param currentUser
	*  @param dhRemindLoanSub  
	*  @see com.zjdiepu.www.ibiz.zpp.DaiHouYwService#diepuFangKuanHuanKuan(com.zjdiepu.www.base.core.AbstractSysUser, com.zjdiepu.www.model.dh.dto.DhRemindLoanSub)  
	*  @version  V1.0
	*/
	@Override
	public void diepuFangKuanHuanKuan(AbstractSysUser currentUser,
			DhRemindLoanSub dhRemindLoanSub) {
		
		
		String userId = currentUser.getId();
		String name = currentUser.getName();
		String busiId = dhRemindLoanSub.getBusiId();
		
		//查询业务信息表数据
		YwBusiness ywBusiness = ywBusinessMapper.selectByPrimaryKey(busiId);
		if(null == ywBusiness || ("").equals(ywBusiness))
		{
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),"ywBusinessMapper is null");
		}
		
		DhRemindLoan dhRemindLoan = new DhRemindLoan();
		dhRemindLoan.setBusiId(busiId);
		dhRemindLoan.setDpStatus(1);
		dhRemindLoan = dhRemindLoanMapper.selectRefined(dhRemindLoan);
		if(null == dhRemindLoan || ("").equals(dhRemindLoan))
		{
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),"dhRemindLoan is null");
		}
		
		dhRemindLoanSub.initInsertData(userId, name);
		dhRemindLoanSub.setRemindLoanId(dhRemindLoan.getId());
		dhRemindLoanSub.setCusId(ywBusiness.getCustomerId());
		int count = dhRemindLoanSubMapper.insert(dhRemindLoanSub);
		if(count < 1)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"dhRemindLoanSub is fail");
		}
		
		String sqlXmls = "select count(t.id) count from dh_remind_loan_sub t where t.busi_id = '"+busiId+"' and t.remind_loan_type = '"+dhRemindLoanSub.getRemindLoanType()+"' and t.dp_status = 1";
		int remindCount = publicDao.countSqlXmls(sqlXmls);
		
		//放款提醒
		if(dhRemindLoanSub.getRemindLoanType() == 1)
		{
			dhRemindLoan.setCountRemindLoan(remindCount);
		}
		//还款提醒
		else if(dhRemindLoanSub.getRemindLoanType() == 2)
		{
			dhRemindLoan.setCountRemindRepayment(remindCount);
		}
		
		count = dhRemindLoanMapper.updateByPrimaryKeySelective(dhRemindLoan);
		if(count < 1)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"dhRemindLoanMapper is fail");
		}
	}
}
